<template>
  <el-dialog
    :title="!dataForm.bid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="商品编号" prop="gid">
        <el-input v-model="dataForm.gid" placeholder="商品编号"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="gname">
        <el-input v-model="dataForm.gname" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="bstartdate">
        <el-input
          v-model="dataForm.bstartdate"
          placeholder="开始时间"
        ></el-input>
      </el-form-item>
      <el-form-item label="结束时间" prop="benddate">
        <el-input v-model="dataForm.benddate" placeholder="结束时间"></el-input>
      </el-form-item>
      <el-form-item label="原价" prop="gprice">
        <el-input v-model="dataForm.gprice" placeholder="原价"></el-input>
      </el-form-item>
      <el-form-item label="底价" prop="bjprice">
        <el-input v-model="dataForm.bjprice" placeholder="底价"></el-input>
      </el-form-item>
      <el-form-item label="规则" prop="brules">
        <el-input v-model="dataForm.brules" placeholder="规则"></el-input>
      </el-form-item>
      <el-form-item label="砍几刀或者能砍多少钱" prop="countorlow">
        <el-input
          v-model="dataForm.countorlow"
          placeholder="砍几刀或者能砍多少钱"
        ></el-input>
      </el-form-item>
      <el-form-item label="详情描述" prop="detail">
        <el-input v-model="dataForm.detail" placeholder="详情描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        bid: 0,
        gid: "",
        gname: "",
        bstartdate: "",
        benddate: "",
        gprice: "",
        bjprice: "",
        brules: "",
        countorlow: "",
        detail: "",
      },
      dataRule: {
        gid: [{ required: true, message: "商品编号不能为空", trigger: "blur" }],
        gname: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        bstartdate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        benddate: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        gprice: [{ required: true, message: "原价不能为空", trigger: "blur" }],
        bjprice: [{ required: true, message: "低价不能为空", trigger: "blur" }],
        brules: [{ required: true, message: "规则不能为空", trigger: "blur" }],
        countorlow: [
          {
            required: true,
            message: "砍几刀或者能砍多少钱不能为空",
            trigger: "blur",
          },
        ],
        detail: [
          { required: true, message: "详情描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.bid = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.bid) {
          this.$http({
            url: this.$http.adornUrl(
              `/bargain/bargain/info/${this.dataForm.bid}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.gid = data.bargain.gid;
              this.dataForm.gname = data.bargain.gname;
              this.dataForm.bstartdate = data.bargain.bstartdate;
              this.dataForm.benddate = data.bargain.benddate;
              this.dataForm.gprice = data.bargain.gprice;
              this.dataForm.bjprice = data.bargain.bjprice;
              this.dataForm.brules = data.bargain.brules;
              this.dataForm.countorlow = data.bargain.countorlow;
              this.dataForm.detail = data.bargain.detail;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/bargain/bargain/${!this.dataForm.bid ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              bid: this.dataForm.bid || undefined,
              gid: this.dataForm.gid,
              gname: this.dataForm.gname,
              bstartdate: this.dataForm.bstartdate,
              benddate: this.dataForm.benddate,
              gprice: this.dataForm.gprice,
              bjprice: this.dataForm.bjprice,
              brules: this.dataForm.brules,
              countorlow: this.dataForm.countorlow,
              detail: this.dataForm.detail,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
